﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Eraile
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;


namespace ChouteZemUp.Engine
{
    public class GameObject : DrawableGameComponent
    {
        #region Variables
        /// <summary>
        /// Variables
        /// </summary>
        protected Texture2D mSprite;
        protected Vector2   mPosition;
        protected Rectangle mBBox;
        protected Rectangle mRenderedRect;
        /// <summary>
        /// Variables Statiques
        /// </summary>
        static public SpriteBatch sSpriteBatch;
        static public Microsoft.Xna.Framework.Game sGame;
        static public ContentManager sContent;
        #endregion


        #region Constructeur
        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="pSpritePath"></param>
        /// <param name="pPositionInitiale"></param>
        /// <param name="pContent"></param>
        /// <param name="pGame"></param>
        public GameObject(String pSpritePath, Vector2 pPositionInitiale, ContentManager pContent, Microsoft.Xna.Framework.Game pGame)
            : base(pGame)
        {
            if(pSpritePath != "")
                mSprite = pContent.Load<Texture2D>(pSpritePath);

            mPosition = pPositionInitiale;

            if (mSprite != null)
            {
                mRenderedRect = new Rectangle(
                                                    (int)0,
                                                    (int)0,
                                                    (int)mSprite.Width,
                                                    (int)mSprite.Height
                                                  );

                mBBox = mRenderedRect;
            }

           
        }
        #endregion


        #region Getters/Setters
        public Texture2D Sprite
        {
            get { return mSprite; }
            set { mSprite = value; }
        }

        public Vector2 Position
        {
            get { return mPosition; }
            set { mPosition = value; }
        }

        public Rectangle BoundingBox
        {
            get { return mBBox; }
            set { mBBox = value; }
        }

        public Rectangle RenderRectangle
        {
            get { return mRenderedRect; }
            set { mRenderedRect = value; }
        }
        #endregion

#region Overrides des fonctions du DrawableGameComponent (Initialize, LoadContent, Update, Draw)
        #region Initialize
        public override void Initialize()
        {
            base.Initialize();
        }
        #endregion

        #region ContentManagement
        protected override void LoadContent()
        {        
            base.LoadContent();
            sSpriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
        }
        #endregion

        #region Update
        public override void Update(GameTime pTime)
        {
            base.Update(pTime);
        }
        #endregion

        #region Draw
        public override void Draw(GameTime pTime)
        {
            base.Draw(pTime);

            if (mSprite == null)
                return;

            sSpriteBatch.Begin();
            sSpriteBatch.Draw(mSprite, mPosition, RenderRectangle, Color.White);
            sSpriteBatch.End();

        }
        #endregion
#endregion

        #region Fonctions (Collisions)
        public IntersectionInfo Intersect(GameObject pOther)
        {
            IntersectionInfo intersection = new IntersectionInfo(this, pOther);

            return intersection;
        }
        #endregion
    }
}
